package com.zhang.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.zhang.yygh.common.result.R;
import com.zhang.yygh.hosp.service.DepartmentService;
import com.zhang.yygh.hosp.utils.HttpRequestHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RequestMapping(value = "/api/hosp")
@RestController
public class ApiDepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //科室删除
    @PostMapping("/department/remove")
    public R remove(HttpServletRequest httpServletRequest){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(httpServletRequest.getParameterMap());
        departmentService.remove(paramMap);
        return R.ok().code(200);
    }

    //科室分页
    @PostMapping(value = "/department/list")
    public R getDepartmentPage(HttpServletRequest httpServletRequest){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(httpServletRequest.getParameterMap());
        //TODO:sign key
        Page page=departmentService.getDepartmentPage(paramMap);
        String string = JSONObject.toJSONString(page);
        Map map = JSONObject.parseObject(string, Map.class);
        return R.ok().code(200).data(map);
    }

    //上传科室信息
    @RequestMapping(value = "/saveDepartment")
    public R saveDepartment(HttpServletRequest httpServletRequest){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(httpServletRequest.getParameterMap());
        //TOTO:sign key校验
        departmentService.saveDepartment(paramMap);
        return R.ok().code(200);
    }
}
